package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.ThreadLocalUtil;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.BusinessException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    //分类分页
    @Override
    public PageResult findByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        //开启分页
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        //查询
        List<Category> list=categoryMapper.findByPage(categoryPageQueryDTO);
        Page<Category> page= (Page<Category>) list;
        //返回结果
        return new PageResult(page.getTotal(),page.getResult());
    }

    //新增分类
    @Override
    public void insert(CategoryDTO categoryDTO) {
        //参数接收
        String name = categoryDTO.getName();
        Integer sort = categoryDTO.getSort();
        //参数校验
        if (StrUtil.isBlank(name)||StrUtil.isBlank(sort.toString())){
            throw new BusinessException("非法参数");
        }
        //业务校验
        if (categoryMapper.findByname(categoryDTO.getName())!=null) {
            throw new BusinessException("分类已存在");
        }
        //dto->entity
        Category category = BeanUtil.copyProperties(categoryDTO, Category.class);
        //补全参数
        category.builder().status(StatusConstant.DISABLE)
                        .createTime(LocalDateTime.now())
                                .updateTime(LocalDateTime.now())
                                        .createUser(ThreadLocalUtil.getCurrentId())
                                                .updateUser(ThreadLocalUtil.getCurrentId());
        //新增
        categoryMapper.insert(category);

    }

    //删除分类
    @Override
    public void deleteById(Long id) {
        //根据分类id统计菜品数量
        Integer dishCount=dishMapper.count(id);
        if (dishCount>0){
            throw  new BusinessException("该分类有菜品,无法删除");
        }
        //根据分类id统计套餐数量
        Integer setMealCount=setmealMapper.count(id);
        if (setMealCount>0){
            throw  new BusinessException("该分类有套餐,无法删除");
        }
        //删除
        categoryMapper.deleteById(id);
    }

    //根据类型查询
    @Override
    public List<Category> getlist(Integer type) {
        // 1.将type封装到dto
        CategoryPageQueryDTO categoryPageQueryDTO = new CategoryPageQueryDTO();
        categoryPageQueryDTO.setType(type);
        // 2.调用mapper查询并返回
        return categoryMapper.findByPage(categoryPageQueryDTO);
    }

    //根据id修改
    @Override
    public void updateById(Category category) {
        //设置修改人
        category.setCreateUser(ThreadLocalUtil.getCurrentId());
        //设置修改时间
        category.setUpdateTime(LocalDateTime.now());
        //修改
        categoryMapper.updateById(category);
    }

    //分类启用/禁用
    @Override
    public void startOrStop(Integer status, Long id) {
        // 封装对象，补全信息
        Category category = Category.builder()
                .status(status)
                .id(id)
                .updateTime(LocalDateTime.now())
                .updateUser(ThreadLocalUtil.getCurrentId())
                .build();
        categoryMapper.updateById(category);
    }

    // 条件查询
    @Override
    public List<Category> getParamList(CategoryDTO categoryDTO) {
        return categoryMapper.getParamList(categoryDTO);
    }


}
